15. Source: APIs (Application Programming Interfaces)

Source: APIs (Application Programming Interfaces)

Introduction

Source: APIs (Application Programming Interfaces)

APIs and Access Libraries

Source: APIs (Application Programming Interfaces) 2

MediaWiki API

MediaWiki has a great tutorial on their website on how their API calls are structured. It's a nice and simple example and they explain the various moving parts:

  • The endpoint (important takeaway: there is nothing special about this URL!)
  • The format
  • The action
  • Action-specific parameters

Go and read that example and then come back to the classroom.

Done reading? Great! Though they say that is a "simple example," it could definitely be simpler! This is where access libraries, also known as client libraries or even just libraries (as in "Twitter API libraries"), come into play and make our lives easier.

wptools Library

There are a bunch of different access libraries for MediaWiki to satisfy the variety of programming languages that exist. Here is a list for Python. This is pretty standard for most APIs. Some libraries are better than others, which again, is standard. For a MediaWiki, the most up to date and human readable one in Python is called wptools . The analogous relationship for Twitter is:

  • MediaWiki API → wptools
  • Twitter API → tweepy

wptools has an even simpler tutorial on their GitHub page using the Mahatma Gandhi Wikipedia page as a working example.

To get a page object, the usage is as follows:

page = wptools.page('Mahatma_Gandhi')

…where 'Mahatma_Gandhi' is the last bit of the Wikipedia URL for that page ( https://en.wikipedia.org/wiki/Mahatma_Gandhi) . This page object has methods that can get us various pieces of data about that Wikipedia page, including all of the images on the page. To get all of the data:

Simply calling get() on a page will automagically fetch extracts, images, infobox data, wikidata, and other metadata via the MediaWiki, Wikidata, and RESTBase APIs.

page = wptools.page('Mahatma_Gandhi').get()

Or if you already have a page object assigned to page :

page.get()

page now has the following attributes, which can be accessed using dot notation through .data :

page.data['image'] , for example, would return a list of data for six images on this specific Wikipedia page.

Quiz

In the Jupyter Notebook below, get the page object for the E.T. The Extra-Terrestial Wikipedia page. Here is the E.T. Wikipedia page for easy reference.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter
  • Opened files (when workspace is loaded): n/a

Quiz: APIs

QUESTION:

Get your code to work in the Jupyter Notebook above, then copy and paste the line of code that assigns a wptools page object to the variable page for the movie E.T. The Extra-Terrestrial, starting with and including page = .

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer